package com.cxm.qyyz.ui.login;

import android.Manifest;
import android.content.Intent;
import android.content.res.Configuration;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.cxm.qyyz.R;
import com.cxm.qyyz.base.activity.BaseActivity;
import com.cxm.qyyz.contract.MainContract;
import com.cxm.qyyz.presenter.MainPresenter;
import com.cxm.qyyz.utils.AppUtil;
import com.cxm.qyyz.utils.ExitUtil;
import com.cxm.qyyz.utils.flow.LogUtils;
import com.cxm.qyyz.widget.plus.StatusBarPlus;

import java.util.Arrays;
import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

/**
 * 时间 ： 2022-1-18 09:34:24
 * 修改内容：  首页显示方案 修改为一次显示 永久显示
 * 修改人 ： yzx
 */
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};

    private final ViewPager2.OnPageChangeCallback pageCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            modifyState(position);
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEvents() {
    }

    @Override
    protected void initInjector() {
    }

    @Override
    public void onReload() {
    }

    @Override
    protected void initStatusBar() {
        StatusBarPlus.setTransparent(this);
        StatusBarPlus.setStatusBarMode(this, true);
    }

    @Override
    protected void releaseEvents() {
        super.releaseEvents();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        setIntent(intent);

        LogUtils.d(" --------------123456-----" + "onNewIntent");
        processIntent(intent);
    }

    private void processIntent(Intent intent) {
    }


    private void checkPermission() {
        if (EasyPermissions.hasPermissions(this, permissions)) {
//            mPresenter.downLoad(this, mApkUrl, null);
        } else {
            int DOWNLOAD_CODE = 100;
            EasyPermissions.requestPermissions(
                    new PermissionRequest.Builder(this, DOWNLOAD_CODE, permissions)
                            .setTheme(R.style.PermissionTheme)
                            .setRationale(R.string.text_permisson_update)
                            .setPositiveButtonText(R.string.text_permissions_apply)
                            .setNegativeButtonText(R.string.text_permissions_cancel)
                            .build());
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @androidx.annotation.NonNull List<String> perms) {
        if (requestCode != 100) {
            return;
        }
        List<String> data = Arrays.asList(permissions);
//        if (perms.containsAll(data)) {
//            mPresenter.downLoad(MainActivity.this, mApkUrl, null);
//        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @androidx.annotation.NonNull List<String> perms) {
        if (requestCode != 100) {
            return;
        }
        if (EasyPermissions.somePermissionDenied(this, permissions) || EasyPermissions.somePermissionPermanentlyDenied(this, Arrays.asList(permissions))) {
            new AppSettingsDialog.Builder(this)
                    .setThemeResId(R.style.PermissionTheme)
                    .setTitle(R.string.text_permissions_preference)
                    .setRationale(R.string.text_permissions_setting)
                    .setPositiveButton(R.string.text_permissions_apply)
                    .setNegativeButton(R.string.text_permissions_cancel)
                    .setRequestCode(100)
                    .build()
                    .show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
//            if (EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//                mPresenter.downLoad(MainActivity.this, mApkUrl, null);
//            }
        }
    }

    private void notifyChild() {
    }



    @Override
    protected void onStart() {
        super.onStart();
//        if (null != init) {
//            init.quitLoginPage();
//        }
    }

    @Override
    public void onBackPressed() {
        if (ExitUtil.enableExit()) {
            AppUtil.getInstance().exitApp();
        } else {
            toast(R.string.hint_exit);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getDemoNumber();
    }

    // 添加未读数变化监听，add 为 true 是添加，为 false 是撤销监听。
    // 退出界面时，必须撤销，以免造成资源泄露



    private void modifyState(int state) {
//        if (tabImageList != null && tabImageList.size() > 0) {
//            for (int i = 0; i < tabImageList.size(); i++) {
//                if (i == state) {
//                    tabImageList.get(i).setSelected(true);
//                    tabTextList.get(i).setTextColor(ContextCompat.getColor(mActivity, R.color.color_333333));
//                } else {
//                    tabImageList.get(i).setSelected(false);
//                    tabTextList.get(i).setTextColor(ContextCompat.getColor(mActivity, R.color.color_cccccc));
//                }
//            }
//        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LogUtils.d("---onConfigurationChanged--MainActivity-");
    }
}